import { Col, Row } from 'antd';
import { connect } from 'umi';
import { UserManageContext, useUserManageRendecer } from './hooks';
import { IUserManageController } from './type';
import cls from 'classnames';
import './styles.less';
import { UserLeftTreeView, UserRightTableContentView } from './components';
import { userManageReq } from './hooks/userManageServices';
import React from 'react';

const UserManageController: React.FC<IUserManageController> = ({ permissions }) => {
  const userFixCls = 'user-manage-';
  const rightTableRef = React.useRef<Record<string, any>>();

  const { reloadLeftTree, userTreeData, userTreeLoading, reloadRightTable, userTableData, userTableLoading, refreshManageTable } = userManageReq(permissions);
  const {
    searchValueTree,
    selectTreeKeys,
    expandedTreeKeys,
    userManDispatch,
    autoExpandParent,
    rightTablePagination,
    leftTreeType,
    searchFormValues,
  } = useUserManageRendecer();

  // 右侧表格刷新方法
  const rightRoladTabieUser = () => {
    // let params: any = {
    //   ...leftTreeType,
    //   ...searchFormValues,
    //   ...rightTablePagination,
    // };
    // reloadRightTable(params);
    refreshManageTable();
  };

  return (
    <UserManageContext.Provider
      value={{
        curRes: permissions, //用户权限
        userFixCls, //样式根类名
        leftTreeData: userTreeData, //左侧树数据
        userTreeLoading, //左侧树loading
        searchValueTree, //左侧树搜索值
        selectTreeKeys, //左侧树选中值
        expandedTreeKeys, //左侧树展开值
        autoExpandParent, //左侧树是否可以自动展开
        rightTablePagination, //右侧分页器参数
        leftTreeType, //左侧点击后的参数
        searchFormValues, //右侧表格表单搜索
        reloadLeftTree, //刷新左侧树
        userManDispatch, // 派发
        reloadRightTable, //获取右侧表格数据
        rightRoladTabieUser, //操作后右侧表格刷新方法
      }}
    >
      <Row style={{ position: 'relative', overflow: 'hidden' }} className={cls(userFixCls + 'row')} gutter={[8, 0]}>
        <Col span={6} className={cls(userFixCls + 'user-col')}>
          <UserLeftTreeView
            leftCheckReloadRight={() => {
              rightTableRef?.current?.userAddEditForm();
            }}
          />
        </Col>
        <UserRightTableContentView userTableData={userTableData} userTableLoading={userTableLoading} ref={rightTableRef} />
      </Row>
    </UserManageContext.Provider>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  permissions: baseLogin?.permissions,
  navTab,
}))(UserManageController);
